package com.huiquan.analysis.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.analysis.constants.AnalysisDictionaryConstant;
import com.huiquan.analysis.domain.AnalysisDictionary;
import com.huiquan.analysis.domain.AnalysisElementSplitAmbiguity;
import com.huiquan.framework.utils.CollectionUtil;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 词典 dao
 *
 * @author leo
 * @time 2017年4月11日 下午5:44:35
 */
@Repository
public class AnalysisDictionaryDao extends BaseDao {
	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "ANALYSIS_DICTIONARY.";

	/**
	 * 批量插入词典数据
	 * 
	 * @param list
	 */
	public void insertAnalysisDictionaryBatch(List<AnalysisDictionary> list) {
		this.insert(space + "INSERT_ANALYSIS_DICTIONARY_BATCH", list, sqlMapClient);
	}

	/**
	 * 检索得到字典数量
	 * 
	 * @param param
	 * @return
	 */
	public int selectAnalysisDictionaryCount(Map<String, Object> param) {
		return (int) this.object(space + "SELECT_ANALYSIS_DICTIONARY_COUNT", param, sqlMapClient);
	}
	
	/**
	 * 检索得到歧义字典数量
	 * 
	 * @param param
	 * @return
	 */
	public int selectAnalysisElementSplitAmbiguityCount(Map<String, Object> param) {
		return (int) this.object(space + "SELECT_ANALYSIS_ELEMENT_SPLIT_AMBIGUITY_COUNT", param, sqlMapClient);
	}		

	/**
	 * 检索得到字典列表
	 * 
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AnalysisDictionary> selectAnalysisDictionaryList(Map<String, Object> param) {
		return (List<AnalysisDictionary>) this.list(space + "SELECT_ANALYSIS_DICTIONARY_LIST", param, sqlMapClient);
	}
	
	/**
	 * 检索得到元素拆分歧义字典列表
	 * 
	 * @param param
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<AnalysisElementSplitAmbiguity> selectAnalysisElementSplitAmbiguityList(Map<String, Object> param) {
		return (List<AnalysisElementSplitAmbiguity>) this.list(space + "SELECT_ANALYSIS_ELEMENT_SPLIT_AMBIGUITY_LIST", param, sqlMapClient);
	}

	/**
	 * 根据id检索得到一条字典
	 * 
	 * @param id
	 * @return
	 */
	public AnalysisDictionary selectAnalysisDictionarById(int id) {
		return (AnalysisDictionary) this.object(space + "SELECT_ANALYSIS_DICTIONARY_BY_ID", id, sqlMapClient);
	}

	/**
	 * 更新一条词典
	 * 
	 * @param entity
	 */
	public void updateAnalysisDictionary(AnalysisDictionary entity) {
		this.update(space + "UPDATE_ANALYSIS_DICTIONARY", entity, sqlMapClient);
	}

	/**
	 * 逻辑删除一条词典
	 * 
	 * @param param
	 */
	public void deleteAnalysisDictionaryById(Map<String, Object> param) {
		this.update(space + "DELETE_ANALYSIS_DICTIONARY_BY_ID", param, sqlMapClient);
	}

	/**
	 * 插入一条词典
	 * 
	 * @param entity
	 */
	public void insertAnalysisDictionary(AnalysisDictionary entity) {
		this.insert(space + "INSERT_ANALYSIS_DICTIONARY", entity, sqlMapClient);
	}

	/**
	 * 根据词语检索外部词典中的词典个数
	 * 
	 * @param vocabulary
	 * @return
	 */
	public int selectOutsideDictionaryCountByVocabulary(String vocabulary) {
		return (int) this.object(space + "SELECT_OUTSIDE_DICTIONARY_COUNT_BY_VOCABULARY", vocabulary, sqlMapClient);
	}

	/**
	 * 根据词语逻辑删除外部词典中的词典
	 * 
	 * @param vocabulary
	 */
	public void deleteOutsideDictionaryByVocabulary(String vocabulary) {
		this.update(space + "DELETE_OUTSIDE_DICTIONARY_BY_VOCABULARY", vocabulary, sqlMapClient);
	}

	/**
	 * 把时间词字典数据插入到临时表
	 * 
	 * @param list
	 */
	public void insertTimeDicToTemp(List<AnalysisDictionary> list) {
		this.insert(space + "INSERT_TIME_DIC_TO_TEMP", list, sqlMapClient);
	}

	/**
	 * 初始化时间词
	 */
	public void initTimeDic() {
		this.update(space + "INIT_TIME_DIC", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisDictionary> selectAnalysisDictionaryByType(Map<String, Object> param) {
		return (List<AnalysisDictionary>) this.list(space + "SELECT_ANALYSIS_DICTIONARY_BY_TYPE", param, sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> checkRepeat() {
		return (List<String>) this.list(space + "CHECK_ANALYSIS_DICTIONARY_REPEAT", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<String> checkConflict() {
		return (List<String>) this.list(space + "CHECK_ANALYSIS_DICTIONARY_CONFLICT", sqlMapClient);
	}

	@SuppressWarnings("unchecked")
	public List<AnalysisDictionary> retrieveAvailableList(String vocabulary, int type) {
		Map<String, Object> param = new HashMap<>();
		param.put("vocabulary", vocabulary);

		// 获取当前类型对应的可用类型
		String availableTypes = "";
		if (AnalysisDictionaryConstant.availableSymptomList.contains(type)) {
			availableTypes = CollectionUtil.joinStringByList(AnalysisDictionaryConstant.availableSymptomList, ",");
		} else if (AnalysisDictionaryConstant.availableDiagnosisList.contains(type)) {
			availableTypes = CollectionUtil.joinStringByList(AnalysisDictionaryConstant.availableDiagnosisList, ",");
		} else {
			return new ArrayList<>();
		}
		param.put("availableTypes", availableTypes);

		return (List<AnalysisDictionary>) this.list(space + "RETRIEVE_AVAILABLE_LIST", param, sqlMapClient);
	}

	public void deleteUnavailableByVocabulary(AnalysisDictionary dictionary, int type) {

		String unavailableTypes = "";
		if (AnalysisDictionaryConstant.availableSymptomList.contains(type)) {
			unavailableTypes = CollectionUtil.joinStringByList(AnalysisDictionaryConstant.unavailableSymptomList, ",");
		} else if (AnalysisDictionaryConstant.availableDiagnosisList.contains(type)) {
			unavailableTypes = CollectionUtil.joinStringByList(AnalysisDictionaryConstant.unavailableDiagnosisList,
					",");
		}

		Map<String, Object> param = new HashMap<>();
		param.put("vocabulary", dictionary.getVocabulary());
		param.put("unavailableTypes", unavailableTypes);
		param.put("modifierId", dictionary.getModifierId());
		param.put("modifierName", dictionary.getModifierName());

		this.delete(space + "DELETE_UNAVAILABLE_BY_VOCABUALRY", param, sqlMapClient);
	}
}
